﻿CREATE TABLE [dbo].[AttRefs] (
    [ID]          INT IDENTITY (1, 1) NOT FOR REPLICATION NOT NULL,
    [EntityID]    INT NOT NULL,
    [AttID]       INT NOT NULL,
    [ReferenceID] INT NOT NULL,
    CONSTRAINT [PK_AttRefs] PRIMARY KEY CLUSTERED ([ID] ASC),
    CONSTRAINT [FK__AttRefs__AttID__114A936A] FOREIGN KEY ([AttID]) REFERENCES [dbo].[Attributes] ([ID]),
    CONSTRAINT [FK_AttRefs_AttRefs] FOREIGN KEY ([EntityID]) REFERENCES [dbo].[Entities] ([EntityID]),
    CONSTRAINT [FK_AttRefs_Entities_Incoming] FOREIGN KEY ([ReferenceID]) REFERENCES [dbo].[Entities] ([EntityID])
);


GO
CREATE NONCLUSTERED INDEX [ix_EntityID_AttID]
    ON [dbo].[AttRefs]([EntityID] ASC, [AttID] ASC)
    INCLUDE([ID], [ReferenceID]);


GO
CREATE NONCLUSTERED INDEX [ix_AttID]
    ON [dbo].[AttRefs]([AttID] ASC);

